/**
* MapsCast
* Applicast(tm) Widget
*
* (c) 2009 Estanislau Trepat / ILERRED, S.L. / INSON, S.A.
*/

var MapsCastViewFullScreen = Class.create(MapsCastView, {
  initialize: function($super, options) {
    $super(options);

    this.createMap();
    if ( this.options.address !== '' )
      this.loadMap(this.options.address);
    else
      this.map.setCenter(new AppliMaps_LatLng(40.4636670, -3.7492200)).updateDisplay();

    this.changeOperationMode(MapsCastViewOperationMode.IDLE);

    this.focusedControl = 0;
    this.focusableControls = ['search', 'map'];
    this.focusControl(this.focusedControl);
  },

  setup: function() {
    // logo
    $('mapscast-logo').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-logo.png');

    // search box
    $('bg-search').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-search.png');

    // map box
    $('bg-mapdetails').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapdetails.png');
    $('bg-mapbox').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapbox.png');
    $('bg-map-loading').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-loading.png');

    // map controls
    $('bg-mapcontrols').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapcontrols.png');
    $('zoom-lever').loadImage(this.options.assetsPath + 'icons/zoom.png').setPos(0, 19*8 - 14*8);
  },

  createMap: function() {
    if ( !AppliMaps_Config.isKeySet() ) return;
    this.map = new AppliMaps_Map('bg-map', {
      size: {w: 588, h: 390},
      format: 'png8',
      maptype: 'mobile',
      zoom: 14
    });
  },

  loadMap: function(address, saveOnSuccess) {
    if ( !AppliMaps_Config.isKeySet() ) return;
    try {
      MapsCastGeocoder.get(address, {
        onGeocoderSuccess: function(response) {
          this.map.resetMarkers();
          for(var i=0, m=response.Placemark.length; i < m; ++i) {
            var coords = response.Placemark[i].Point.coordinates;
            this.map.addMarker(new AppliMaps_MapMarker(new AppliMaps_LatLng(coords[1], coords[0]), 'mid', null, 'red'));
          }
          this.map.updateDisplay();

          if ( saveOnSuccess )
            setRegistry('Item1', address);

          $('q').setStr(address);
          this.options.address = address;
        }.bind(this),
        onGeocoderError: function(error) {
          alert(error.toString());
        }
      });
    } catch(e) {
      print(String(e));
    }
  },

  changeOperationMode: function(mode) {
    if ( mode < 0 || mode >= MapsCastViewOperationModes.length ) return;

    switch ( MapsCastViewOperationModes[mode] ) {
      case 'idle':
        $('bg-search').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-search.png');
        $('bg-mapbox').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapbox.png');
        $('map-controls').setAlpha(80);
        $('bg-mapdetails').setAlpha(80);
        break;

      case 'search':
        $('bg-search').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-search-selected.png');
        $('bg-mapbox').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapbox.png');
        $('map-controls').setAlpha(80);
        $('bg-mapdetails').setAlpha(80);
        break;

      case 'map':
        $('bg-search').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-search.png');
        $('bg-mapbox').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapbox-selected.png');
        $('map-controls').setAlpha(255);
        $('bg-mapdetails').setAlpha(255);
        break;
    }

    this.currentMode = mode;
  },

  focusControl: function(control) {
    if ( control < 0 || control >= this.focusableControls.length ) return;

    switch ( this.focusableControls[control] ) {
      case 'search':
        $('bg-search').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-search-focus.png');
        $('bg-mapbox').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapbox.png');
        break;

      case 'map':
        $('bg-search').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-search.png');
        $('bg-mapbox').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapbox-focus.png');
        break;

      default:
        $('bg-search').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-search.png');
        $('bg-mapbox').loadImage(this.options.assetsPath + 'icons/bg-fullscreen-mapbox.png');
        break;
    }

    this.focusedControl = control;
  },

  promptForLocation: function() {
    var text = prompt('', this.options.address, true);
    if ( text ) {
      var stripped = String(text).strip();
      if ( stripped !== '' )
        this.loadMap(stripped, true);
    }

    this.changeOperationMode(MapsCastViewOperationMode.IDLE);
    this.focusControl(this.focusedControl);
  },

  onKeyPress: function(e) {
    if ( !e ) return;

    switch ( e.keyCode ) {
      case Event.KEY_CONFIRM:
        switch ( MapsCastViewOperationModes[this.currentMode] ) {
          case 'idle':
            switch ( this.focusableControls[this.focusedControl] ) {
              case 'search':
                this.changeOperationMode(MapsCastViewOperationMode.SEARCH);
                this.promptForLocation();
                break;

              case 'map':
                this.changeOperationMode(MapsCastViewOperationMode.MAP);
                break;
            }
            break;

          case 'map':
            this.changeOperationMode(MapsCastViewOperationMode.IDLE);
            this.focusControl(this.focusedControl);
            break;
        }
        break;

      case Event.KEY_UP:
        switch ( MapsCastViewOperationModes[this.currentMode] ) {
          case 'idle':
            this.focusedControl++;
            if ( this.focusedControl >= this.focusableControls.length )
              this.focusedControl = 0;
            this.focusControl(this.focusedControl);
            break;

          case 'map':
            this.map.panNorth(60).updateDisplay();
            break;
        }
        break;

      case Event.KEY_DOWN:
        switch ( MapsCastViewOperationModes[this.currentMode] ) {
          case 'idle':
            this.focusedControl--;
            if ( this.focusedControl < 0 )
              this.focusedControl = this.focusableControls.length - 1;
            this.focusControl(this.focusedControl);
            break;

          case 'map':
            this.map.panSouth(60).updateDisplay();
            break;
        }
        break;

      case Event.KEY_LEFT:
        switch ( MapsCastViewOperationModes[this.currentMode] ) {
          case 'idle':
            break;

          case 'map':
            this.map.panWest(100).updateDisplay();
            break;
        }
        break;

      case Event.KEY_RIGHT:
        switch ( MapsCastViewOperationModes[this.currentMode] ) {
          case 'idle':
            break;

          case 'map':
            this.map.panEast(100).updateDisplay();
            break;
        }
        break;


      // Zoom controls
      case Event.KEY_RED:
        if ( MapsCastViewOperationModes[this.currentMode] === 'map' ) {
          this.map.zoomIn().updateDisplay();
          $('zoom-lever').setPos(0, 19*8 - this.map.currentZoom()*8);
        }
        break;

      case Event.KEY_GREEN:
        if ( MapsCastViewOperationModes[this.currentMode] === 'map' ) {
          this.map.zoomOut().updateDisplay();
          $('zoom-lever').setPos(0, 19*8 - this.map.currentZoom()*8);
        }
        break;

      // Map type controls
      case Event.KEY_YELLOW:
        if ( MapsCastViewOperationModes[this.currentMode] === 'map' )
          this.map.setType('mobile').updateDisplay();
        break;

      case Event.KEY_BLUE:
        if ( MapsCastViewOperationModes[this.currentMode] === 'map' )
          this.map.setType('hybrid').updateDisplay();
        break;
    }
  }
});